export default {
  namespaced: true,
  state: () => ({
    address: {},
    token: uni.getStorageSync('token') || '',
    // 用户的信息
    userInfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 重定向信息
    redirectInfo: null
  }),
  mutations: {
    // 更新收货地址
    updateAddress(state, address) {
      uni.setStorageSync('address', JSON.stringify(address))
      state.address = address
    },
    // 重新加载时从本地获取地址信息
    getLocalAddress(state) {
      state.address = JSON.parse(uni.getStorageSync('address') || '{}')
    },
    updateUserInfo(state, userInfo) {
      state.userInfo = userInfo
      
      // 将用户信息保存到本地
      this.commit('moduleUser/saveUserInfoToStorage')
    },
    saveUserInfoToStorage(state) {
      uni.setStorageSync('userInfo', JSON.stringify(state.userInfo))
    },
    // 更新token
    updateToken(state, token) {
      state.token = token
      // 保存token信息到本地
      this.commit('moduleUser/saveTokenToStorage')
    },
    saveTokenToStorage(state) {
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
    removeLocalInfo(state) {
      state.address = {}
      state.userInfo = {}
      state.token = ''
      
      uni.removeStorageSync('address')
      uni.removeStorageSync('userInfo')
      uni.removeStorageSync('token')
    },
    // 登陆完毕跳回之前的页面
    updateRedirectInfo(state, info) {
      state.redirectInfo = info
      console.log('重定向信息：', state.redirectInfo)
    }
  },
  getters: {
    addstr(state) {
      if(!state.address.provinceName) return ''
      return state.address.provinceName +  state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}